package com.zking.frame;

import com.zking.dao.FoodDao;
import com.zking.entity.Food;
import com.zking.utils.廖全箐.Evaluate;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class Client extends JFrame {
    private JPanel jPanel;
    private JScrollPane jScrollPane;
    private JLabel jLabel, jLabel2, jLabel3;
    private JLabel jTextField, jTextField2;
    private JButton jButton;
    private JButton jButton2;
    private JButton jButton3;
    private List<Food> foods;
    public void setFoods(List<Food> foods) {
        this.foods = foods;
    }

    private JPanel j;

    public Client(JFrame jFrame, String names, String name) {
        jFrame.dispose();

        System.out.println(names+" "+name);
        // 设置名字
        setTitle("吃了没后台");
        // 设置背景颜色
//        getContentPane().setBackground(new Color(214,255,128));
        // 设置窗口大小
        setSize(1400, 1000);
        // 取消布局
        setLayout(null);
        // 设置居中
        setLocationRelativeTo(null);

        foods = See(names, name);
        ImageIcon i = new ImageIcon(foods.get(0).getShow());
        jLabel = new JLabel(i, 0);
        jLabel.setBounds(0, 10, 1385, 60);
        jLabel.setFont(new Font("黑体", Font.BOLD, 40));
        add(jLabel, 0);

        // 设置背景图片
        ImageIcon imageIcon = new ImageIcon("picture/12.jpg");
        JLabel jLabel12 = new JLabel(imageIcon);
        jLabel12.setBounds(0, 0, 1400, 1000);
        add(jLabel12, -1);

        ImageIcon i2 = new ImageIcon(foods.get(0).getStoreaddress());
        jLabel2 = new JLabel(i2, JLabel.RIGHT);
        jLabel2.setBounds(250, 50, 200, 180);
        jLabel2.setFont(new Font("黑体", Font.BOLD, 20));
        add(jLabel2, 0);

        jLabel3 = new JLabel(name, JLabel.CENTER);
        jLabel3.setBounds(430, 110, 500, 50);
        jLabel3.setForeground(new Color(232,146,71));
        jLabel3.setFont(new Font("叶根友毛笔行书2.0版", Font.BOLD, 40));
        add(jLabel3, 0);

        jButton = new JButton("添加菜品");
        jButton.setBounds(950, 95, 110, 50);
        jButton.setFont(new Font("黑体", Font.BOLD, 15));
        add(jButton, 0);

        jButton2 = new JButton("订单管理");
        jButton2.setBounds(1100,95,110,50);
        jButton2.setFont(new Font("黑体",Font.BOLD,15));
        add(jButton2,0);

        jButton3 = new JButton("评价管理");
        jButton3.setBounds(1250,95,110,50);
        jButton3.setFont(new Font("黑体",Font.BOLD,15));
        add(jButton3,0);


        j = getJPanel(names,name);
        jScrollPane = new JScrollPane(j);
        jScrollPane.setBounds(110, 190, 1160, 750);
        add(jScrollPane, 0);

        jButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
               if (jButton.getText().equals("添加菜品")){
                   new Backstage(Client.this,true,"添加数据",names,name);
                   j = getJPanel(names,name);
                   jScrollPane.setViewportView(j);
                   // 加载并刷新面板
                   jScrollPane.repaint();
                   jScrollPane.updateUI();
                   jScrollPane.revalidate();
               }else{
                   jButton3.setEnabled(true);
                   j = getJPanel(names,name);
                   jScrollPane.setViewportView(j);
                   jButton.setText("添加菜品");
                   // 加载并刷新面板
                   jScrollPane.repaint();
                   jScrollPane.updateUI();
                   jScrollPane.revalidate();
               }
            }

        });

        jButton3.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                jButton.setText("菜品管理");
                j = new Evaluate().getEvaluate(name,Client.this,jScrollPane);
                jScrollPane.setViewportView(j);
                jButton3.setEnabled(false);
                // 加载并刷新面板
                jScrollPane.repaint();
                jScrollPane.updateUI();
                jScrollPane.revalidate();
            }

        });

        // 设置窗口不可调节
        setResizable(false);
        // 设置窗口是否可见
        setVisible(true);
        // 设置窗口关闭模式
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }

    public List<Food> See(String sort, String shop) {
        List<Food> foods = new ArrayList<Food>();
        FoodDao foodDao = new FoodDao();
        foods = foodDao.queryFoodName(sort, shop);
        return foods;
    }

    public JPanel getJPanel(String names, String name){
        JPanel jPanel = new JPanel();
        jPanel.setPreferredSize(new Dimension(1100, foods.size()*190));
        jPanel.setLayout(null);
        for (int a = 0; a < foods.size(); a++) {
            JPanel jPanel1 = new JPanel();
            jPanel1.setBounds(0, 190*a+10, 1200, 180);
            jPanel1.setLayout(null);
            jPanel1.setBackground(Color.white);


            JLabel jLabel = new JLabel("菜品名：");
            jLabel.setBounds(205, 5, 150, 30);
            jLabel.setFont(new Font("黑体", Font.BOLD, 20));
            jPanel1.add(jLabel);

            JLabel jLabel1 = new JLabel(foods.get(a).getName());
            jLabel1.setBounds(205, 40, 800, 50);
            jLabel1.setFont(new Font("黑体", Font.BOLD, 30));
            jLabel1.setForeground(new Color(41, 180, 246));
            jPanel1.add(jLabel1);

            ImageIcon imageIcon1 = new ImageIcon(foods.get(a).getPictureaddress());
            JLabel jLabel2 = new JLabel(imageIcon1);
            jLabel2.setBounds(0, 0, 200, 190);
            jLabel2.setFont(new Font("黑体", Font.BOLD, 20));
            jPanel1.add(jLabel2);

            JLabel jLabel4 = new JLabel("商品价格：");
            jLabel4.setBounds(205, 95, 400, 30);
            jLabel4.setFont(new Font("黑体", Font.BOLD, 20));
            jPanel1.add(jLabel4);

            JLabel jLabel5 = new JLabel("￥"+foods.get(a).getPrice());
            jLabel5.setBounds(205, 130, 800, 50);
            jLabel5.setFont(new Font("黑体", Font.BOLD, 30));
            jLabel5.setForeground(Color.red);
            jPanel1.add(jLabel5);

            JButton jButton = new JButton("修改");
            jButton.setBounds(1000, 40, 100, 40);
            jButton.setFont(new Font("黑体", Font.BOLD, 15));
            jPanel1.add(jButton);


            JButton jButton1 = new JButton("删除");
            jButton1.setFont(new Font("黑体", Font.BOLD, 15));
            jButton1.setBounds(1000, 130, 100, 40);
            jPanel1.add(jButton1);
            jPanel.add(jPanel1);


            jButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    new Upate(Client.this,true,"修改数据",names,name,jLabel1.getText());
                    j = getJPanel(names,name);
                    jScrollPane.setViewportView(j);
                    // 加载并刷新面板
                    jScrollPane.repaint();
                    jScrollPane.updateUI();
                    jScrollPane.revalidate();
                }
            });
            /*
            删除商品
             */
            jButton1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    System.out.println(  jLabel1.getText());
                         //  foods.get(a).getName();
                        new FoodDao().DeleteFood( jLabel1.getText());
                        setFoods(See(names, name));
                        j = getJPanel(names,name);
                        jScrollPane.setViewportView(j);
                        // 加载并刷新面板
                        jScrollPane.repaint();
                        jScrollPane.updateUI();
                        jScrollPane.revalidate();
                }
            });
        }
        return jPanel;
    }

}
